<?php


/*
 Open the connection to the database
*/
$conn = mysql_connect('studentdb.gl.umbc.edu', 'chrimo1', 'chrimo1');
if (!$conn) {
   die('Could not connect');
}

$db_selected = mysql_select_db('chrimo1', $conn);
if (!$db_selected) {
   die ('Can\'t use chrimo1');
}


/*
 Get all of the items the user had in the cart when they confirmed checkout
*/
$sql = "SELECT * FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."'";
$cartResult = mysql_query($sql, $conn);
if (mysql_num_rows($cartResult)==0){
  die();
}
$orders = "";
$cost = 0;

$cr = mysql_fetch_array($cartResult);
$sqlGame = "SELECT * FROM gameInventory WHERE idNum=".$cr['gameId'];
$gameResult = mysql_query($sqlGame, $conn);
$gr = mysql_fetch_array($gameResult);
$orders = $orders." ".$gr['gameName'];
$cost = $cost + $cr['cost'];

while($cr = mysql_fetch_array($cartResult))
{
  $sqlGame = "SELECT * FROM gameInventory WHERE idNum=".$cr['gameId'];
  $gameResult = mysql_query($sqlGame, $conn);
  $gr = mysql_fetch_array($gameResult);
  $orders = $orders.", ".$gr['gameName'];
  $cost = $cost + $cr['cost'];
}

$cost = $cost * 1.06;

$sql = "SELECT * FROM customerInfo WHERE username = '".$_COOKIE['user']."'";
$custResults = mysql_query($sql, $conn);
$custInfo = mysql_fetch_array($custResults);

/*
 Post the order to the orderInfo table
*/
$sql = "INSERT INTO orderInfo ( custIdNum, items, totalCost) VALUES ('".$custInfo['custIdNum']."', '".$orders."', '".$cost."')";
$insert = mysql_query($sql, $conn);


/*
 Clear the users shopping cart
*/
$sql = "DELETE FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."'";
mysql_query($sql, $conn);

?>




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Legacy-Gamer</title>
<!-- InstanceEndEditable -->

<link rel="stylesheet" type="text/css" href="legacyGamer.css">
</head>
<body class="body">


<div id="Header" class="style1">
	<div id="sublayer1" >
		<a href="index.php" style="float:left;"><img src="images/Title.png" height="115px" alt="store logo"></a>
		
		<a href="cart.php" style="float:right; height:115px; border-left:1px solid #ccc; position:relative;">
			<img src="images/cart.png" alt="store logo">
			<?php
				/*
				  Identify the number of items in the shopping cart(should be 0)
				*/
				$sql = "SELECT * FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."'";
				$result = mysql_query($sql, $conn);
				echo '<h2 style="position: absolute; top:10px">&nbsp &nbsp'.mysql_num_rows($result).'</h2>';

			?>
		</a>
		<?php
			echo '<div style="font-size:10pt; color:#FFC80E; float:right;" align="right">Welcome, '.$_COOKIE["user"].'<br><a href="login.php?logout=1" >Logout</a></div>';
		?>
		
	</div>	
</div>
	
<div id="Layer2" class="style2" align="left" >
	<ul id="nav">
	  <h3 style="height:10px; color:#FFC80E;">Genre:</h3>
	  <li2><a href="listItems.php?id=BeatemUp">
 		Beat'em Up
	  </a></li2>
	  <li2><a href="listItems.php?id=RPG">
 		RPG
	  </a></li2>
	  <li2><a href="listItems.php?id=Platform">
 		Platform
	  </a></li2>
	  <li2><a href="listItems.php?id=Action">
 		Action
	  </a></li2>
	  <li2><a href="listItems.php?id=PuzzleStrategy">
 		Puzzle/Strategy
	  </a></li2>
	  <li2><a href="listItems.php?id=ShootemUp">
 		Shoot'em Up
	  </a></li2>
	  <li2><a href="listItems.php?id=CityBuildingSimulator">
 		City Building Simulator
	  </a></li2>
	  <li2><a href="listItems.php?id=ScrollingShooter">
 		Scrollling Shooter
	  </a></li2>
	  <li2><a href="listItems.php?id=Racing">
 		Racing
	  </a></li2>
	  <li2><a href="listItems.php?id=Sports">
 		Sports
	  </a></li2>
	  
	  <h3 style="height:10px; color:#FFC80E;">System:</h3>
	  <li2><a href="listItems.php?id=Sega">
 		Sega Genesis
	  </a></li2>
	  <li2><a href="listItems.php?id=SuperNintendo">
 		Super Nintendo
	  </a></li2>

	  <h3 style="height:10px; color:#FFC80E;">Player Modes:</h3>
	  <li2><a href="listItems.php?id=Single">
 		Single-Player
	  </a></li2>
	  <li2><a href="listItems.php?id=Multi">
 		MultiPlayer
	  </a></li2>
	  <li2><a href="listItems.php?id=Coop">
 		Cooperative
	  </a></li2>

	</ul>
</div>

<div id="Layer3" style="margin-left:5px; margin-top:0px; position:absolute; height:625px; z-index:1; left:175px; top:115px; overflow:inheret; max-width:89%; min-width:680px;" align="left"> 
  <form method="get" action="search.php">
    <input style="width:72%;" type="text" name="search">  
    <input type="submit" value="Search">
    
    
  </form>
  <div align="center">
    <h1 style="align:center; color:#FFC80E;">Thank you for your order! We will process it as soon as possible!</h1>
    <br>
    <br>
  </div>
</div>

</body>



<body class="style2">
</body>
<!-- InstanceEnd --></html>
